<?php
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.0
 */
 
defined('_JEXEC') or die;
 
if (!class_exists('VirtueMartModelOrders')) require ( JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'orders.php');
 
class PmapiModelOrder extends VirtueMartModelOrders
{
    private function getVendorCurrencyCode($vendorId)
    {
        if (!class_exists('VirtueMartModelVendor')) require ( JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'vendor.php');
        $currency = VirtueMartModelVendor::getVendorCurrency($vendorId);
        
        return  $currency->currency_code_3;
    } 
      
 
    private function getVendorUserId($vendorId)
    {
        if (!class_exists('VirtueMartModelvendor')) require ( JPATH_VM_ADMINISTRATOR.DS.'models'.DS.'vendor.php');
        
        return VirtueMartModelvendor::getUserIdByVendorId($vendorId); 
    }

       
    private function getCountryName($id)
    {
        static $_countries = array();
        
        if( !isset($_countries[$id])){
            $model = $this->getModel('country');
            $model->setId($id);
            $country = $model->getData($id);
            $_countries[$id] = $country?$country->country_name:'';
        }

        return $_countries[$id]; 
    }
    
    
    private function getStateName($id)
    {
        static $_states = array();
        
        if( !isset($_states[$id])){
            $model = $this->getModel('state');
            $model->setId($id);
            $state = $model->getSingleState();
            $_states[$id] = $state?$state->state_name:'';
        }
        
        return $_states[$id]; 
    }


    private function addVcardInfo($u)
    {
        if (empty($u)) return $this;
        
        // build a name        
        $fn = empty($u->first_name)?'':$u->first_name;
        if (!empty($u->middle_name)) $fn .= ' '.$u->middle_name;
        if (!empty($u->last_name)) $fn .= ' '.$u->last_name;
        $fn = trim( $fn);
        $u->fn = $fn;
        
        // ann country and state in clear
        if (!empty($u->virtuemart_country_id)) $u->country = $this->getCountryName($u->virtuemart_country_id);
        if (!empty($u->virtuemart_state_id)) $u->state = $this->getStateName($u->virtuemart_state_id);
        
        return $this;       
    }
    
    
    public function getItem()
    {
        $id = JFactory::getApplication()->input->get('id',null);
        $order = (object) $this->getMyOrderDetails($id);

        // Check for errors. t leas an item pre order required.
        if (count($errors = $this->get('Errors')) || empty($order) || !count($order->items)) {
            throw new Exception(implode("\n", $errors)||'Not Found', 404);
        }

        // Inject additional data to order
        $order->buyer = (object) $order->details['BT'];
        $order->buyer->deliveryInfo = (object) $order->details['ST'];
        unset($order->details);
        foreach($order->items as $item ){
            $item->product_subtotal_without_tax = $item->product_subtotal_with_tax - $item->product_tax;
            $item->taxRate = (round(($item->product_subtotal_with_tax/$item->product_subtotal_without_tax),5)-1)*100;
        }
        $this->addVcardInfo($order->buyer);
        $this->addVcardInfo($order->buyer->deliveryInfo);

        $vendor = new stdClass;
        $vendor->virtuemart_vendor_id = $order->buyer->virtuemart_vendor_id;
        $vendor->virtuemart_user_id = $this->getVendorUserId($vendor->virtuemart_vendor_id);
        $vendor->currencyCode = $this->getVendorCurrencyCode($vendor->virtuemart_vendor_id);
        $order->vendor = $vendor;
        
        return $order;
    }        
}   